﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Input;
using System.Globalization;
using System.Reflection.Metadata;

namespace HackWinMine
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
        private const int WM_HOTKEY = 0x0312;
        private const int MOD_NONE = 0x0000;
        private const int MOD_ALT = 0x0001;
        private const int MOD_CONTROL = 0x0002;
        private const int MOD_SHIFT = 0x0004;
        private const int MOD_WIN = 0x0008;

        WinMineCrack winMineCrack;
        HwndSource hwndSource;
        const int hotKeyId = 9001;
        //参考https://learn.microsoft.com/zh-cn/windows/win32/inputdev/virtual-key-codes
        private const uint VK_CAPITAL = 0x14;
        private const uint VK_VOLUME_MUTE = 0xAD;
        private const uint VK_VOLUME_DOWN = 0xAE;
        	
        public MainWindow()
        {
            InitializeComponent();
            InitializeGrid();
            winMineCrack = new WinMineCrack();
            //Loaded += MainWindow_Loaded;
            //Closed+= MainWindow_Closed;
        }


        private void InitializeGrid()
        {
            int cellSize = 12; // 每个方格的大小，根据需要调整

            // 清除现有的子控件
            gridMain.Children.Clear();

            for (int i = 0; i < WinMineData.WMN_FRAME_ROWS; i++)
            {
                for (int j = 0; j < WinMineData.WMN_FRAME_COLUMNS; j++)
                {
                    // 创建一个新的Rectangle
                    Rectangle rectangle = new Rectangle
                    {
                        Width = cellSize,
                        Height = cellSize,
                        Stroke = Brushes.Black, // 方格边框颜色
                        StrokeThickness = 1,
                        Margin = new Thickness(1) // 方格之间的间距
                    };

                    // 设置Rectangle的位置
                    Grid.SetRow(rectangle, i);
                    Grid.SetColumn(rectangle, j);

                    // 将Rectangle添加到Grid中
                    gridMain.Children.Add(rectangle);
                }
            }
        }

       

        protected override void OnClosed( EventArgs e)
        {
            base.OnClosed(e);
            if (hwndSource != null)
            {
                IntPtr handle = new WindowInteropHelper(this).Handle;
                // 注销热键
                UnregisterHotKey(handle, hotKeyId);
            }
                
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            bool ret;
            base.OnSourceInitialized(e);
            IntPtr handle = new WindowInteropHelper(this).Handle;
            hwndSource = HwndSource.FromHwnd(handle);
            if (hwndSource != null)
            {
                hwndSource.AddHook(WndProc);
                ret = RegisterHotKey(handle, hotKeyId, MOD_NONE, VK_VOLUME_MUTE);
                ret = RegisterHotKey(handle, hotKeyId, MOD_NONE, VK_VOLUME_DOWN);
                if (ret == false)
                {
                    MessageBox.Show("RegisterHotKey err");
                }
            }
            else
            {
                MessageBox.Show("FromHwnd err");
            }
        }
        
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            int ret=0;
            if (msg == WM_HOTKEY)
            {
                switch ((int)wParam)
                {
                    case hotKeyId:
                        int key = (int)lParam >> 16 & 0xFFFF;
                        if (key == VK_VOLUME_MUTE)
                            ret = winMineCrack.CrackNext();
                        else if (key == VK_VOLUME_DOWN)
                            ret = winMineCrack.Crack();
                        if (ret != 0)
                        {
                            MessageBox.Show("找不到扫雷程序");
                        }
                        else
                        {
                            ShowMineData();
                        }
                        
                        handled = true;
                        break;
                }
            }
            return IntPtr.Zero;
        }
    


        private void BtnReadDataSeg_Click(object sender, RoutedEventArgs e)
        {
            int ret = winMineCrack.RefreshData();

            if (ret != 0)
            {
                MessageBox.Show("找不到扫雷程序");
                return;
            }
            ShowMineData();
        }

        //窗口大小为{<17,82>,<400,450>}，格子宽23
        private void BtnCrack_Click(object sender, RoutedEventArgs e)
        {
            int ret = winMineCrack.Crack();
            if (ret != 0)
            {
                MessageBox.Show("找不到扫雷程序");
                return;
            }
            ShowMineData();
        }

        //在雷区显示地雷位置
        void ShowMineData()
        {
            for (int i = 0; i < WinMineData.WMN_FRAME_ROWS; i++)
            {
                for (int j = 0; j < WinMineData.WMN_FRAME_COLUMNS; j++)
                {
                    WinMineData.ENUM_MINE_DATA frameCellData = winMineCrack.getFrameData(i, j);
                    Color color = Colors.Green;
                    switch (frameCellData)
                    {
                        case WinMineData.ENUM_MINE_DATA.FRAME:
                            color = Colors.Green;
                            break;
                        case WinMineData.ENUM_MINE_DATA.MINE:
                            color = Colors.Purple;
                            break;
                        case WinMineData.ENUM_MINE_DATA.EMPTY:
                            color = Colors.White;
                            break;
                        default:
                            color = Colors.Yellow;
                            break;
                    }
                    Rectangle rectangle = gridMain.Children[i * WinMineData.WMN_FRAME_COLUMNS + j] as Rectangle;
                    if (rectangle != null)
                    {
                        rectangle.Fill = new SolidColorBrush(color);
                    }
                }
            }
        }
    }
}
